package cn.youyeku.zsxh.ui.fragment;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.getbase.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.adapter.RefreshFootAdapter;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.bean.Help;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.ui.fragment.ABS.AbsSwipeFragment;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by youye on 17-3-26.
 */

public class FgmMutualHelp extends AbsSwipeFragment implements View.OnClickListener {

    private FloatingActionButton fgm_mutual_help_btn_post, fgm_mutual_help_btn_accept;

    public FgmMutualHelp() {
        super(R.layout.fgm_mutual_help, R.id.fgm_mutual_platform_recycle, R.id.fgm_mutual_platform_swipe);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected void initView() {
        fgm_mutual_help_btn_post = (FloatingActionButton) view.findViewById(R.id.fgm_mutual_help_btn_post);
        fgm_mutual_help_btn_accept = (FloatingActionButton) view.findViewById(R.id.fgm_mutual_help_btn_accept);
        fgm_mutual_help_btn_post.setOnClickListener(this);
        fgm_mutual_help_btn_accept.setOnClickListener(this);
    }

    @Override
    protected RefreshFootAdapter getAapter() {
        return new RefreshFootAdapter<Help>(R.layout.fgm_mutual_help_item, R.layout.refresh_footer) {
            @Override
            protected void onBindCustomerData(RefreshViewHolder holder, Help data, int pos) {
                ImageView img_face = holder.getChildView(R.id.mutual_imgV_head);
                TextView mutual_textV_nickname = holder.getChildView(R.id.mutual_textV_nickname);
                TextView mutual_textV_time = holder.getChildView(R.id.mutual_textV_time);
                TextView mutual_textV_repay = holder.getChildView(R.id.mutual_textV_repay);
                TextView mutual_textV_address = holder.getChildView(R.id.mutual_textV_address);
                TextView mutual_textV_event = holder.getChildView(R.id.mutual_textV_event);

                Glide.with(getContext())
                        .load(XhApi.getFace(data.getHelp_author_uid()))
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .error(R.drawable.default_avatar)
                        .into(img_face);
                mutual_textV_repay.setText("回报" + data.getHelp_reward());
                mutual_textV_address.setText("地址" + data.getHelp_address());
                mutual_textV_event.setText("事件" + data.getHelp_content());
                mutual_textV_nickname.setText("昵称" + data.getNick_name());
                mutual_textV_time.setText("时间：" + data.getHelp_time());

            }

            @Override
            protected void setmOnItemClick(View itemView, final Help data, int pos) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                           UIControl.showActMutualHelp(getContext(), data);

                    }
                });

                itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {

                        return false;
                    }
                });
            }


        };
    }

    @Override
    protected void requestData() {

        XhApi.get_helps(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();

                JSONObject object = null;
                try {
                    object = new JSONObject(data);
                    int code = object.getInt("code");

                    LogUtils.w("code  == " + code + data);
                    if(code==200){
                        Gson gson = new Gson();
                        mData = gson.fromJson(object.getString("data"), new TypeToken<List<Help>>() {
                        }.getType());
                    }
                    Message msg = new Message();
                    msg.what = code;
                    handler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fgm_mutual_help_btn_post:
                UIControl.showActMutualHelpPost(getContext());
                break;
            case R.id.fgm_mutual_help_btn_accept:
                UIControl.showActMutualAccepted(getContext());
                break;

        }

    }
}
